package com.raychi.demo.servlet.orders;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.raychi.demo.entity.Orders;
import com.raychi.demo.util.JdbcUtil;

public class OrderSevlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			List<Orders> list = getOrders();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/index4.jsp").forward(request, response);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	private List<Orders> getOrders() throws ClassNotFoundException, SQLException {
		List<Orders> list = new ArrayList<Orders>();
		Connection conn = JdbcUtil.getConnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("select * from orders");
		Orders orders = null;
		while(rs.next()){
			orders = new Orders();
			orders.setName(rs.getString("name"));
			orders.setTotalPrice(rs.getFloat("totalprice"));
			list.add(orders);
		}
		return list;
	}
}
